﻿using CRM.Models;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Z.EntityFramework.Plus;
using CRM.Enums;

namespace CRM.Services
{
    public class RuleTemplateService : BaseService
    {
        private readonly MyDbContext _dbContext;

        public RuleTemplateService(MyDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public record ChooseData(int Id, string Name, States state);

        /// <summary>
        /// 获取当前公司下所有权限模板
        /// </summary>
        /// <returns></returns>
        public List<ChooseData> GetAllRuleTemplate(int CompanyId,int OemId)
        {
            return _dbContext.RuleTemplate.
                Where(i => i.CompanyId == CompanyId).
                Where(i => i.OemId == OemId).
                Select(n => new ChooseData
            (
                n.Id,
                n.Name,
                n.State
            )).ToList();
        }

        /// <summary>
        /// 权限模板
        /// </summary>
        /// <param name="id"></param>
        /// <param name="CompanyId"></param>
        /// <param name="OemId"></param>
        /// <returns></returns>
        public RuleTemplate GetRuleTemplateInfo(int id, int CompanyId, int OemId) {
            return _dbContext.RuleTemplate.
                Where(m => m.Id == id).
                Where(i => i.CompanyId == CompanyId).
                Where(i => i.OemId == OemId).
                FirstOrDefault();
        }

        /// <summary>
        /// 初始化权限模板
        /// </summary>
        /// <param name="RuleTemplateModel"></param>
        /// <param name="CompanyId"></param>
        /// <param name="OemId"></param>
        /// <returns></returns>
        public async Task<RuleTemplate> InitRuleTemplateAsync(RuleTemplate RuleTemplateModel, int CompanyId, int OemId)
        {
            RuleTemplateModel.Name = "初始_权限模板";
            RuleTemplateModel.Remark = "";
            RuleTemplateModel.State = States.ENABLE;
            RuleTemplateModel.Sort = -Helper.GetNowTimeStamp();
            RuleTemplateModel.CompanyId = CompanyId;
            RuleTemplateModel.OemId = OemId;
            RuleTemplateModel.Id = default;
            _dbContext.RuleTemplate.Attach(RuleTemplateModel);
            await _dbContext.RuleTemplate.AddAsync(RuleTemplateModel);

            await _dbContext.SaveChangesAsync();

            return RuleTemplateModel;
        }

        /// <summary>
        /// 更新权限模版权限信息
        /// </summary>
        /// <param name="RuleTemplateData"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public int UpdatePermission(RuleTemplate RuleTemplateData, int Id) {
            var dic = new Dictionary<string, object>
            {
                ["SystemManager"] = RuleTemplateData.SystemManager,
                ["UserManager"] = RuleTemplateData.UserManager,
                ["DataSearch"] = RuleTemplateData.DataSearch,
                ["CustomerManager"] = RuleTemplateData.CustomerManager,
                ["EmailMarketing"] = RuleTemplateData.EmailMarketing,
                ["SocialMarketingTasks"] = RuleTemplateData.SocialMarketingTasks,
                ["DataMarketing"] = RuleTemplateData.DataMarketing,
                ["BuyersData"] = RuleTemplateData.BuyersData
            };
            return _dbContext.RuleTemplate.Where(m => m.Id == Id).Update(dic);
        }
    }
}
